package encheres.ihm;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.BoxLayout;

public class ListeObjets extends JPanel
{
	private JList vue;
	
	private DefaultListModel modele;
	
	private ArrayList<HashMap> objets;
	
	//private HashMap<HashMap, JPanel> objets;
	
	public ListeObjets(String titre)
	{
		setBorder(new TitledBorder(null, titre, TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(new BorderLayout(0, 0));
		
		modele = new DefaultListModel();
		
		vue = new JList(modele);
		add(vue, BorderLayout.NORTH);
		// main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
		
		objets = new ArrayList<HashMap>();
		//objets = new HashMap<HashMap, JPanel>();
	}
	
	public synchronized HashMap getSelectedObject()
	{
		if (vue.getSelectedIndex() == -1) return null;
		return objets.get(vue.getSelectedIndex());
	}
	
	public synchronized void addObjet(HashMap obj, ActionListener ecouteurBoutonDemarrer, 
										ActionListener ecouteurBoutonRetirer)
	{
		int id = (Integer) obj.get("id");
		String nom = (String) obj.get("nom");
		//Long prixDepart = (Long) obj.get("prixDepart");
		//Long prixReserve = (Long) obj.get("prixReserve");
		// String description = (String) obj.get("description");
		
		//String toolTipText = "prix de depart : " + prixDepart + 
		//					", prix de reserve : " + prixReserve;
		
		/*
		JPanel pan = new JPanel(new FlowLayout(FlowLayout.LEFT));
			JLabel lab = new JLabel(nom);
		pan.add(lab);
			JButton but = new JButton("Demarrer enchere");
			but.addActionListener(ecouteurBoutonDemarrer);
		pan.add(but);
			but = new JButton("Retirer");
			but.addActionListener(ecouteurBoutonRetirer);
		pan.add(but);
		pan.setToolTipText(toolTipText);
		
		main.add(pan);
		*/
		
		modele.addElement("" + id + " - " + nom);
		objets.add(obj);
		// main.add
		//objets.put(obj, pan);
	}

	public synchronized void removeObjet(HashMap obj)
	{
		objets.remove(obj);
		modele.removeElement("" + obj.get("id") + " - " + obj.get("nom"));
		
		/*
		main.remove(objets.get(obj));
		objets.remove(obj);
		*/
	}
}
